package com.wenhao.sort;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane;

public class HeapSort {
    public static void main(String[] args) {

    }
    public void HeapSort (int[] array) {
        int length = array.length - 1;
        // buildHeap
        for (int i = length / 2;i >= 0;i--) {
            adjustHeapDown(array,0,length);
        }
        for (int i = length;i > 0;i--) {
            int temp = array[i];
            array[i] = array[0];
            array[0] = temp;
            length--;
            adjustHeapDown(array,0,length);
        }
    }
    public void adjustHeapDown (int[] array,int index,int end) {
        int child = index * 2;
        if (child >= end) {
            return;
        }
        if (child + 1 < end && array[child] < array[child + 1]) {
            child++;
        }
        array[index] = Math.max(array[index],array[child]);
        adjustHeapDown(array,child,end);
    }
}
